package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Dialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.blankj.utilcode.util.VibrateUtils;
import com.stark.irremote.lib.base.IrComRemoteController;
import com.stark.irremote.lib.base.IrConst;
import com.stark.irremote.lib.base.bean.IrRemote;
import com.stark.irremote.lib.base.key.IrFanKey;
import com.stark.irremote.lib.net.IRextReqManager;
import com.stark.irremote.lib.net.bean.IrBrand;
import com.stark.irremote.lib.net.bean.IrRemoteIndex;
import com.stark.irremote.lib.ui.util.IrDialogUtil;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.databinding.ActivityRemoteFsBinding;
import stark.common.basic.retrofit.IReqRetCallback;

public class RemoteFsActivity extends BaseAc<ActivityRemoteFsBinding> {

    //-----风扇-----
    public static boolean isAdd = false;
    public static IrBrand bean;
    public static IrRemote bean2;
    private Dialog myTipDialog;
    private Dialog myResponseDialog;
    private Dialog myReNameDialog;
    //请求到的遥控器索引对象列表
    private List<IrRemoteIndex> mRemoteIndexList = new ArrayList<>();
    private int mCurRemotePos = 0;
    private IrComRemoteController mRemoteController;
    private EditText etDialogRenName;

    @Override
    protected int onCreate() {
        return R.layout.activity_remote_fs;
    }

    @Override
    protected void initView() {
        mDataBinding.ivRemoteFsBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mDataBinding.ivRemoteFsRename.setOnClickListener(this);
        mDataBinding.ivRemoteFsSource.setOnClickListener(this);
        mDataBinding.ivRemoteFsHome.setOnClickListener(this);
        mDataBinding.ivRemoteFsMenu.setOnClickListener(this);
        mDataBinding.ivRemoteFsBack2.setOnClickListener(this);
        mDataBinding.ivRemoteFsShake.setOnClickListener(this);
        mDataBinding.ivRemoteFsPowerReduce.setOnClickListener(this);
        mDataBinding.ivRemoteFsPowerAdd.setOnClickListener(this);
        mDataBinding.ivRemoteFsShang.setOnClickListener(this);
        mDataBinding.ivRemoteFsXia.setOnClickListener(this);
        mDataBinding.ivRemoteFsZuo.setOnClickListener(this);
        mDataBinding.ivRemoteFsYou.setOnClickListener(this);
        mDataBinding.ivRemoteFsRight.setOnClickListener(this);
        //给各相关按键绑定对应的按键值对象
        mDataBinding.ivRemoteFsSource.setTag(IrFanKey.POWER);
        mDataBinding.ivRemoteFsHome.setTag(IrFanKey.HOME);
        mDataBinding.ivRemoteFsMenu.setTag(IrFanKey.MENU);
        mDataBinding.ivRemoteFsBack2.setTag(IrFanKey.BACK);
        mDataBinding.ivRemoteFsShake.setTag(IrFanKey.SHAKE);
        mDataBinding.ivRemoteFsPowerReduce.setTag(IrFanKey.WIND_MINUS);
        mDataBinding.ivRemoteFsPowerAdd.setTag(IrFanKey.WIND_ADD);
        mDataBinding.ivRemoteFsShang.setTag(IrFanKey.UP);
        mDataBinding.ivRemoteFsXia.setTag(IrFanKey.DOWN);
        mDataBinding.ivRemoteFsZuo.setTag(IrFanKey.LEFT);
        mDataBinding.ivRemoteFsYou.setTag(IrFanKey.RIGHT);
        mDataBinding.ivRemoteFsRight.setTag(IrFanKey.OK);
        if (isAdd) {
            //设置名称状态
            mDataBinding.tvRemoteFsName.setText(bean.name + getString(R.string.fs));
            //下载第一个遥控器码库
            reqRemoteIndexAndDownloadFirst();
        } else {
            mDataBinding.tvRemoteFsName.setText(bean2.name);
            mRemoteController = IrComRemoteController.create(bean2);
        }
        //提示弹框
        tipDialog();
        //响应弹框
        responseDialog();
        //重命名弹框
        reNameDialog();
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()){
            case R.id.ivRemoteFsRename://重命名
                etDialogRenName.setText(mDataBinding.tvRemoteFsName.getText().toString());
                myReNameDialog.show();
                break;
            case R.id.ivRemoteFsSource://电源
                VibrateUtils.vibrate(IrConst.VIBRATE_TIME);
                mRemoteController.handleIrKey(IrFanKey.POWER.getValue());
                if (isAdd) {
                    myResponseDialog.show();
                }
                break;
            case R.id.ivRemoteFsHome://返回主页
                VibrateUtils.vibrate(IrConst.VIBRATE_TIME);
                mRemoteController.handleIrKey(IrFanKey.HOME.getValue());
                break;
            case R.id.ivRemoteFsMenu://菜单
                VibrateUtils.vibrate(IrConst.VIBRATE_TIME);
                mRemoteController.handleIrKey(IrFanKey.MENU.getValue());
                break;
            case R.id.ivRemoteFsBack2://返回
                VibrateUtils.vibrate(IrConst.VIBRATE_TIME);
                mRemoteController.handleIrKey(IrFanKey.BACK.getValue());
                break;
            case R.id.ivRemoteFsShake://摇头
                VibrateUtils.vibrate(IrConst.VIBRATE_TIME);
                mRemoteController.handleIrKey(IrFanKey.SHAKE.getValue());
                break;
            case R.id.ivRemoteFsPowerReduce://风力减
                VibrateUtils.vibrate(IrConst.VIBRATE_TIME);
                mRemoteController.handleIrKey(IrFanKey.WIND_MINUS.getValue());
                break;
            case R.id.ivRemoteFsPowerAdd://风力加
                VibrateUtils.vibrate(IrConst.VIBRATE_TIME);
                mRemoteController.handleIrKey(IrFanKey.WIND_ADD.getValue());
                break;
            case R.id.ivRemoteFsShang://上
                VibrateUtils.vibrate(IrConst.VIBRATE_TIME);
                mRemoteController.handleIrKey(IrFanKey.UP.getValue());
                break;
            case R.id.ivRemoteFsXia://下
                VibrateUtils.vibrate(IrConst.VIBRATE_TIME);
                mRemoteController.handleIrKey(IrFanKey.DOWN.getValue());
                break;
            case R.id.ivRemoteFsZuo://左
                VibrateUtils.vibrate(IrConst.VIBRATE_TIME);
                mRemoteController.handleIrKey(IrFanKey.LEFT.getValue());
                break;
            case R.id.ivRemoteFsYou://右
                VibrateUtils.vibrate(IrConst.VIBRATE_TIME);
                mRemoteController.handleIrKey(IrFanKey.RIGHT.getValue());
                break;
            case R.id.ivRemoteFsRight://确定
                VibrateUtils.vibrate(IrConst.VIBRATE_TIME);
                mRemoteController.handleIrKey(IrFanKey.OK.getValue());
                break;
            //提示弹框
            case R.id.ivDialogTipRight://关闭提示弹框
                myTipDialog.dismiss();
                break;
            //响应弹框
            case R.id.tvDialogResOff://无响应
                myResponseDialog.dismiss();
                reqRemoteIndexAndDownloadFirst();
                break;
            case R.id.tvDialogResOn://已响应
                myResponseDialog.dismiss();
                isAdd = false;
                mRemoteController.saveRemote(mDataBinding.tvRemoteFsName.getText().toString());
                etDialogRenName.setText(mDataBinding.tvRemoteFsName.getText().toString());
                myReNameDialog.show();
                break;
            //重命名弹框
            case R.id.ivDialogRenameCancel://取消
                myReNameDialog.dismiss();
                break;
            case R.id.ivDialogRenameRight://确定
                String name = etDialogRenName.getText().toString();
                if (TextUtils.isEmpty(name)) {
                    ToastUtils.showShort(R.string.please_input_name);
                    return;
                }
                myReNameDialog.dismiss();
                mDataBinding.tvRemoteFsName.setText(name);
                mRemoteController.updateRemote(mDataBinding.tvRemoteFsName.getText().toString());
                break;
        }
    }

    @Override
    protected void initData() {

    }

    /**
     * 请求获取某一电器某一品牌下的所有的遥控器数据，并下载第一个遥控器的码库文件
     */
    private void reqRemoteIndexAndDownloadFirst() {
        showDialog(getString(com.stark.common.res.R.string.loading));
        IRextReqManager.irextApi().listIndexes(this, bean.categoryId, bean.id, new IReqRetCallback<List<IrRemoteIndex>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<IrRemoteIndex> result) {
                if (!success) {
                    dismissDialog();
                    if (TextUtils.isEmpty(msg)) {
                        msg = getString(R.string.ir_get_data_exception);
                    }
                    IrDialogUtil.showNoRemoteIdxDialog(mContext, msg, () -> finish());
                    return;
                }
                if (result == null || result.size() == 0) {
                    dismissDialog();
                    IrDialogUtil.showNoRemoteIdxDialog(mContext, getString(R.string.ir_no_remote_data), () -> finish());
                    return;
                }
                mRemoteIndexList = result;
                mCurRemotePos = 0;
                downloadRemoteIndexBin(true, mRemoteIndexList.get(mCurRemotePos));
            }
        });
    }

    /**
     * 下载remoteIndex对应的遥控器码库文件
     *
     * @param isShowLoading 当前是否有加载框在显示
     * @param remoteIndex
     */
    private void downloadRemoteIndexBin(boolean isShowLoading, @NonNull IrRemoteIndex remoteIndex) {
        if (!isShowLoading) {
            showDialog(getString(com.stark.common.res.R.string.loading));
        }
        IRextReqManager.irextApi().downloadBin(remoteIndex, new IReqRetCallback<String>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable String binFilePath) {
                dismissDialog();
                if (binFilePath == null) {
                    ToastUtils.showShort(msg);
                    return;
                }
                //码库文件下载成功，则创建对应的控制器
                mRemoteController = IrComRemoteController.create(remoteIndex);
            }
        });
    }

    //提示弹框
    private void tipDialog() {
        myTipDialog = new Dialog(mContext, R.style.DialogStyle);
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_tip, null);
        myTipDialog.setContentView(inflate);
        //获取当前Activity所在的窗体
        Window dialogWindow = myTipDialog.getWindow();
        //设置Dialog从窗体底部弹出
        dialogWindow.setGravity(Gravity.CENTER);
        DisplayMetrics dm = getResources().getDisplayMetrics();
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (dm.widthPixels * 0.9);
        // 将属性设置给窗体
        dialogWindow.setAttributes(lp);
        ImageView close = inflate.findViewById(R.id.ivDialogTipRight);
        close.setOnClickListener(this);
        if (isAdd) {
            myTipDialog.show();
        }
    }

    //响应弹框
    private void responseDialog() {
        myResponseDialog = new Dialog(mContext, R.style.DialogStyle);
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_response, null);
        myResponseDialog.setContentView(inflate);
        myResponseDialog.setCancelable(false);
        //获取当前Activity所在的窗体
        Window dialogWindow = myResponseDialog.getWindow();
        //设置Dialog从窗体底部弹出
        dialogWindow.setGravity(Gravity.CENTER);
        DisplayMetrics dm = getResources().getDisplayMetrics();
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (dm.widthPixels * 0.9);
        // 将属性设置给窗体
        dialogWindow.setAttributes(lp);
        TextView resOff = inflate.findViewById(R.id.tvDialogResOff);
        TextView resOn = inflate.findViewById(R.id.tvDialogResOn);
        resOff.setOnClickListener(this);
        resOn.setOnClickListener(this);
    }

    //重命名弹框
    private void reNameDialog() {
        myReNameDialog = new Dialog(mContext, R.style.DialogStyle);
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_rename, null);
        myReNameDialog.setContentView(inflate);
        //获取当前Activity所在的窗体
        Window dialogWindow = myReNameDialog.getWindow();
        //设置Dialog从窗体底部弹出
        dialogWindow.setGravity(Gravity.CENTER);
        DisplayMetrics dm = getResources().getDisplayMetrics();
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (dm.widthPixels * 0.9);
        // 将属性设置给窗体
        dialogWindow.setAttributes(lp);
        etDialogRenName = inflate.findViewById(R.id.etDialogRenameText);
        ImageView cancel = inflate.findViewById(R.id.ivDialogRenameCancel);
        ImageView right = inflate.findViewById(R.id.ivDialogRenameRight);
        cancel.setOnClickListener(this);
        right.setOnClickListener(this);
    }

}